﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace KD.Sms.Controls
{
    public partial class LightCurtain : UserControl
    {

        private bool _bIsLeftToRight = true;
        private byte[] _bValues = null;
        private int _iStableCount = 0;
        /// <summary>
        /// 
        /// </summary>
        public LightCurtain()
        {
            InitializeComponent();
            E_ShowFormLeftToRight = true;
            E_StartFromLeftToRight = true;
            E_CarPostion = 0;
        }
        [Category("自定义")]
        [Description("是否从左到右显示,用于显示光幕示意图")]
        [TypeConverter(typeof(bool))]
        [DefaultValue(true)]
        [Browsable(true)]
        public bool E_ShowFormLeftToRight
        {
            set { _bIsLeftToRight = value; DoInitControl(); }
            get { return _bIsLeftToRight; }
        }

        [Category("自定义")]
        [Description("光幕起点是否从左右到右安装，正对光幕区分左右")]
        [TypeConverter(typeof(bool))]
        [DefaultValue(true)]
        [Browsable(true)]
        public bool E_StartFromLeftToRight
        {
            set; get;
        }

        [Category("自定义")]
        [Description("显示的值")]
        [TypeConverter(typeof(byte[]))]
        [Browsable(true)]
        [DefaultValue(null)]
        public byte[] E_Values
        {
            set
            {
                _bValues = value;
                DoSetValue();
            }
            get
            {
                return _bValues;
            }
        }
        [Category("自定义")]
        [Description("确定当前车辆遮挡的位置")]
        [TypeConverter(typeof(bool))]
        [DefaultValue(true)]
        [Browsable(true)]
        public int E_CarPostion { set; get; }


        [Category("自定义")]
        [Description("当前光栅是否稳定")]
        [TypeConverter(typeof(bool))]
        [DefaultValue(true)]
        [Browsable(true)]
        public bool E_IsStable
        {
            get
            {
                return _iStableCount >= 20;
            }
        }

        private byte[] E_ValueLast { set; get; }
        private Panel[] E_ArrPan { set; get; }
        /// <summary>
        /// Panel对象
        /// </summary>
        private Hashtable E_HasPanel { set; get; }
        private Panel E_PanHeart { set; get; }

        private byte[] E_DisplayResults = new byte[28];
        private bool _bShowHeart = false;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LiveCurtain_Load(object sender, EventArgs e)
        {
            DoInitControl();
        }


        /// <summary>
        /// 
        /// </summary>
        private void DoInitControl()
        {
            E_HasPanel = new Hashtable();
            panMain.Height = 12;
            panMain.Controls.Clear();
            //先初始化Pan面板数组
            E_ArrPan = new Panel[28];
            for (int i = 0; i < E_ArrPan.Length; i++)
            {
                Panel pan = new Panel();
                pan.BackgroundImageLayout = ImageLayout.Stretch;
                pan.BackgroundImage = KD.Sms.Properties.Resources.green;
                pan.Size = new Size(12, 12);
                E_ArrPan[i] = pan;
                E_HasPanel.Add(i, pan);
            }

            if (E_ShowFormLeftToRight)
            {
                for (int i = E_ArrPan.Length - 1; i >= 0; i--)
                {
                    E_ArrPan[i].Dock = DockStyle.Left;
                    panMain.Controls.Add(E_ArrPan[i]);
                    Panel pan = new Panel();
                    pan.Width = 1;
                    pan.Dock = DockStyle.Left;
                    panMain.Controls.Add(pan);
                }
            }
            else
            {
                for (int i = 0; i < E_ArrPan.Length; i++)
                {
                    E_ArrPan[i].Dock = DockStyle.Left;
                    panMain.Controls.Add(E_ArrPan[i]);
                    Panel pan = new Panel();
                    pan.Width = 1;
                    pan.Dock = DockStyle.Left;
                    panMain.Controls.Add(pan);
                }
            }
            Panel panSpace = new Panel();
            panSpace.Width = 20;
            panSpace.Dock = DockStyle.Left;
            panMain.Controls.Add(panSpace);

            E_PanHeart = new Panel();
            E_PanHeart.Width = 12;
            E_PanHeart.Dock = DockStyle.Left;
            E_PanHeart.BackgroundImage = KD.Sms.Properties.Resources.green;
            E_PanHeart.BackgroundImageLayout = ImageLayout.Stretch;
            panMain.Controls.Add(E_PanHeart);
        }

        /// <summary>
        /// 
        /// </summary>
        private void DoSetValue()
        {
            if (E_ValueLast == null || !DoCompare(E_Values, E_ValueLast))
            {
                _iStableCount = 0;
                E_DisplayResults = new byte[28];
                DoConvertByteToBit(E_Values[0]).CopyTo(E_DisplayResults, 0);
                DoConvertByteToBit(E_Values[1]).CopyTo(E_DisplayResults, 8);
                DoConvertByteToBit(E_Values[2]).CopyTo(E_DisplayResults, 16);
                for (int i = 0; i < 4; i++)
                {
                    E_DisplayResults[24 + i] = DoConvertByteToBit(E_Values[3])[i];
                }

                for (int i = 0; i < E_DisplayResults.Length; i++)
                {
                    if (E_DisplayResults[i] == 0)
                        UI.FormHelper.SetGroundImage((Panel)E_HasPanel[i], KD.Sms.Properties.Resources.green);
                    else
                        UI.FormHelper.SetGroundImage((Panel)E_HasPanel[i], KD.Sms.Properties.Resources.red);
                }

                E_CarPostion = 0;
                E_ValueLast = E_Values;
                if (E_StartFromLeftToRight)
                {
                    for (int i = E_DisplayResults.Length; i >= 1; i--)
                    {
                        if (E_DisplayResults[i - 1] == 1)
                        {
                            if (i >= 3)
                            {
                                if (E_DisplayResults[i - 1] == 1 && E_DisplayResults[i - 2] == 1)
                                {
                                    E_CarPostion = i;
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < E_DisplayResults.Length; i++)
                    {
                        if (E_DisplayResults[i] == 1)
                        {
                            if (i >= 3)
                            {
                                if (E_DisplayResults[i - 1] == 1 && E_DisplayResults[i - 2] == 1)
                                {
                                    E_CarPostion = i+1;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                _iStableCount++;
                if (_iStableCount > 10000)
                    _iStableCount = 20;
            }

            UI.FormHelper.SetGroundImage(E_PanHeart, _bShowHeart ? KD.Sms.Properties.Resources.grey : KD.Sms.Properties.Resources.green);
            _bShowHeart = !_bShowHeart;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private byte[] DoConvertByteToBit(byte value)
        {
            byte[] _byResult = new byte[8];
            for (int i = 0; i < _byResult.Length; i++)
            {
                _byResult[i] = (byte)((value >> i) & 0x1);
            }
            return _byResult;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_arrNow"></param>
        /// <param name="_arrLast"></param>
        /// <returns></returns>
        private bool DoCompare(byte[] _arrNow, byte[] _arrLast)
        {
            bool _bResult = true;
            for (int i = 0; i < _arrNow.Length; i++)
            {
                if (_arrNow[i] != _arrLast[i])
                {
                    _bResult = false;
                    break;
                }
            }
            return _bResult;
        }

        private void LiveCurtain_SizeChanged(object sender, EventArgs e)
        {
            this.Height = 12;
        }
    }
}
